package jihyun51.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import jihyun51.dao.MemberDAO;
import jihyun51.model.MemberVO;

public class UpdateMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JPasswordField pwd;
	private JTextField uid;
	private JTextField name;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UpdateMember dialog = new UpdateMember("",0,null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UpdateMember(final String  mno, final int r, final JTable table) {
		//회원정보 수정을 위해 ManageMeber에서 전달한 mno를 받기위해
		//UpdateMember의 기본 생성자 대신 mno를 매개변수로 하는 새로운 생성자 작성
		
		//또한 회원정보 수정후 ManageMember의 JTable의 model을 변경해야 하므로 
		//수정해야 할 행 row와 JTable 객체도 생성자 매개변수로 전달
		
		setTitle(" :: 회원수정 ::");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel label = new JLabel("이름");
		
		JLabel label_1 = new JLabel("아이디");
		
		JLabel label_2 = new JLabel("비밀번호");
		
		JLabel label_3 = new JLabel("성별");
		
		JRadioButton radioButton = new JRadioButton("남");
		
		JRadioButton radioButton_1 = new JRadioButton("여");
		radioButton_1.setSelected(true);
		
		pwd = new JPasswordField();
		pwd.setPreferredSize(new Dimension(115, 21));
		
		uid = new JTextField();
		uid.setEnabled(false);
		uid.setColumns(10);
		
		name = new JTextField();
		name.setColumns(10);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
					.addContainerGap(59, Short.MAX_VALUE)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
						.addComponent(label_3, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label_2, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(label, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)))
					.addGap(45)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
							.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(uid, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(radioButton, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(radioButton_1, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))
								.addComponent(pwd, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE))))
					.addGap(83))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(37)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(label)
							.addGap(17)
							.addComponent(label_1)
							.addGap(17)
							.addComponent(label_2)
							.addGap(21))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(11)
							.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(11)
							.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(18)))
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(label_3)
						.addComponent(radioButton)
						.addComponent(radioButton_1))
					.addContainerGap(55, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("수정");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						//수정완료 버튼 클릭시
						//수정할 값을 MemberVO에 저장
						MemberVO m=new MemberVO(
								mno, null, new String(pwd.getPassword()),name.getText(),null,null
								); //MemberDAO의 UpdateMember메서드 호출 후
						// 실행 결과값을 cnt에 저장 
						int cnt = new MemberDAO().updateMember(m);
						//회원정보가 제대로 수정된 경우 수정완료 메세지 창 출력
						if (cnt>0){
							JOptionPane.showMessageDialog(null, "수정완료");
							
							//UpdateMember수정된 값을 manageMember의 JTable에도 적용해야 함
							table.setValueAt(name.getText(),r,2);
							table.repaint();
							dispose();
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("취소");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
					dispose();
					//수정취소 버튼 클릭시
					//회원정보 수정 창 닫음
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		//회원번호를 이용해서 특정회원의 모든 정보를 select 한후 MemberVO에 저장 
		MemberVO m=new MemberDAO().selectOneMember(mno);
		//MemberVo의 값을 컴퍼넌트에 출력
		uid.setText(m.getUid());
		name.setText(m.getName());
		pwd.setText(m.getPwd());
	}//생성자
}
